//
//  BookCityViewController.m
//  225视听百科
//
//  Created by 张元阳(10) on 14-12-27.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "BookCityViewController.h"
#import "ConnectModel.h"
#import "HotListViewController.h"
#import "CustomButton.h"

@interface BookCityViewController ()

@end

@implementation BookCityViewController
- (void)dealloc
{
    [_label release];
    [_bookArray release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.bookArray = [NSMutableArray array];
    self.view.backgroundColor = [UIColor lightGrayColor];
    [self createButton];
}
- (void)createButton
{
    [ConnectModel connectModelWithUrl:@"http://app.9nali.com/753?scale=2&page_id=1&device=iPhone&version=1.1.5" parmaters:nil finished:^(NSData *getData) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:getData options:NSJSONReadingMutableLeaves error:nil];
        NSMutableArray *arr = [dic objectForKey:@"list"];
        [_bookArray addObjectsFromArray:arr];
        NSMutableArray *array = [NSMutableArray arrayWithObjects:@"hot", @"novel", @"music", @"fun", @"comic", @"message", @"emotion", @"history", @"english", @"study", @"talk", @"airing", @"opera", @"child", @"radio", @"business", @"IT", @"healthy", @"other", nil];
        for (int i = 0; i < [array count]; i++) {
            CustomButton *button = [CustomButton buttonWithType:UIButtonTypeSystem];
            button.frame = CGRectMake(10 + (self.view.frame.size.width / 4) * (i % 4), 20 + (self.view.frame.size.width / 4) * (i / 4), 60, 60);
            NSDictionary *dicc = [_bookArray objectAtIndex:i];
            NSString *string = [dicc objectForKey:@"category_id"];
            button.tag = [string intValue];
            button.backgroundColor = [UIColor whiteColor];
            button.layer.cornerRadius = 30;
            button.layer.masksToBounds = YES;
            NSString *str = [NSString stringWithFormat:@"%@.png", [array objectAtIndex:i]];
            UIImage *image1 = [UIImage imageNamed:str];
            UIImageView *imageV = [[UIImageView alloc] initWithImage:image1];
            imageV.frame = CGRectMake(7, 7, 45, 45);
            [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
            [button addSubview:imageV];
            [self.view addSubview:button];
            
            self.label = [[UILabel alloc] initWithFrame:CGRectMake(5 + (self.view.frame.size.width / 4) * (i % 4), 80 + (self.view.frame.size.width / 4) * (i / 4), 70, 20)];
            NSDictionary *dic = [_bookArray objectAtIndex:i];
            _label.text = [dic objectForKey:@"title"];
            button.name = _label.text;
            [_label setFont:[UIFont systemFontOfSize:14]];
            _label.textAlignment = NSTextAlignmentCenter;
            [self.view addSubview:_label];
            [_label release];
            
        }
    }];
    
}
- (void)buttonAction:(CustomButton *)sender
{
    HotListViewController *list = [[HotListViewController alloc] init];
    [self.navigationController pushViewController:list animated:YES];
    list.catetory_id = sender.tag;
    list.name = sender.name;
    [list release];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
